package com.clpms.controller;

import com.clpms.bean.enumBean.ErrorCodeEnum;
import com.clpms.bean.req.DeleteCourseReq;
import com.clpms.bean.req.DeleteLearnRecordReq;
import com.clpms.bean.req.LearnRecordAddReq;
import com.clpms.bean.req.UpdateLearnRecordReq;
import com.clpms.bean.resp.UploadLearnRecordResp;
import com.clpms.service.LearnRecordService;
import com.clpms.utils.CommonException;
import com.clpms.utils.ResultDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Objects;

/**
 * @author caixinyong
 * @date 2023/5/12 3:55 PM
 */

@RestController
@RequestMapping("/learnRecord/v1")
@Slf4j
public class LearnRecordController {

    @Resource
    private LearnRecordService learnRecordService;

    @PostMapping("/queryByWorkId")
    ResultDto<Boolean> queryByWorkId() {
        return ResultDto.Success(true);
    }

    @PostMapping("/addByHand")
    ResultDto<Boolean> addByHand(@RequestBody LearnRecordAddReq req) {
        if (
                Objects.isNull(req.getProgress())
                        && Objects.isNull(req.getScore())
                        && Objects.isNull(req.getCompletionTime())
                        && Objects.isNull(req.getEasyType())
                        && StringUtils.isBlank(req.getRemark())
        ) {
            throw new CommonException(ErrorCodeEnum.PARAM_ERROR.getCode(), "至少填写一项数据");
        }
        Boolean result = learnRecordService.addBatch(Collections.singletonList(req), req.getWorkId());
        return ResultDto.Success(result);
    }

    @PostMapping("/uploadLearnRecordFile")
    public ResultDto<UploadLearnRecordResp> uploadLearnRecordFile(Long workId,
                                                                  @RequestParam("uploadLearnRecordFile") MultipartFile file) {
        UploadLearnRecordResp resp = learnRecordService.uploadLearnRecordFile(workId, file);
        return ResultDto.Success(resp);
    }

    @PostMapping("/deleteBatch")
    public ResultDto<Boolean> deleteBatch(@RequestBody @Valid DeleteLearnRecordReq req) {
        Boolean result = learnRecordService.deleteBatch(req);
        return ResultDto.Success(result);
    }

    @PostMapping("/updateLearnRecord")
    public ResultDto<Boolean> updateLearnRecord(@RequestBody @Valid UpdateLearnRecordReq req) {
        Boolean result = learnRecordService.updateLearnRecord(req);
        return ResultDto.Success(result);
    }
}
